#!/usr/bin/python


# This file is the entry point for the XUL pages. It provides some constants (NAO, PIMO) that will be used by the XUL pages to call the methods from one ontology or the other.


import sys

from python_interface_to_soprano import *

# sys.argv[0] is the name of the script, here python_calls
results_path = sys.argv[1]
method_name = sys.argv[2]
fp = 3 # the first parameter for the function method_name is sys.argv[fp]

i = Interface(results_path)
i.__init__(results_path)

from nao import Nao
from pimo import Pimo

NAO = Nao(i)
PIMO = Pimo(i)

# expected output:
# - nothing if the adding succeeds.
# - the file error.log is generated and contains the error if one occurs.
def addTag(): # params = uri, tag_label, tag_description="", tag_label_language=XMLSCHEMA_STRING
	res = None
	if len(sys.argv) == fp+2:
		res = NAO.addTag(uri=sys.argv[fp+0], tag_label=sys.argv[fp+1])
	elif len(sys.argv) == fp+3:
		res = NAO.addTag(uri=sys.argv[fp+0], tag_label=sys.argv[fp+1], tag_description=sys.argv[fp+2])
	elif len(sys.argv) == fp+4:
		res = NAO.addTag(uri=sys.argv[fp+0], tag_label=sys.argv[fp+1], tag_description=sys.argv[fp+2], tag_label_language=sys.argv[fp+3])
	else:
		print "bad usage..."
		print "usage: "
		print "\tpython_calls results_path function_name [param1 param2 ...]"
		exit(1)
	return res
	
# expected output:
# - nothing if the removal succeeds.
# - the file error.log is generated and contains the error if one occurs.
def removeTag(): # params = uri, tag_label, tag_label_language=XMLSCHEMA_STRING
	res = None
	if len(sys.argv) == fp+2:
		res = NAO.removeTag(uri=sys.argv[fp+0], tag_label=sys.argv[fp+1])
	elif len(sys.argv) == fp+3:
		res = NAO.removeTag(uri=sys.argv[fp+0], tag_label=sys.argv[fp+1], tag_label_language=sys.argv[fp+2])
	else:
		print "bad usage..."
		print "usage: "
		print "\tpython_calls results_path function_name [param1 param2 ...]"
		exit(1)
	return res
	
# expected output:
# - nothing.
def setDescription(): # params = uri, description
	res = None
	if len(sys.argv) == fp+2:
		res = NAO.setDescription(uri=sys.argv[fp+0], description=sys.argv[fp+1])
	else:
		print "bad usage..."
		print "usage: "
		print "\tpython_calls results_path function_name [param1 param2 ...]"
		exit(1)
	return res
	
# expected output:
# - nothing (if the given uri does not have any description, this function simply does nothing).
def removeDescription(): # params = uri
	res = None
	if len(sys.argv) == fp+1:
		res = NAO.removeDescription(uri=sys.argv[fp+0])
	else:
		print "bad usage..."
		print "usage: "
		print "\tpython_calls results_path function_name [param1 param2 ...]"
		exit(1)
	return res
	
# expected output:
# - the file description.txt is generated and contains the requested description if no error occur.
# - the file error.log is generated and contains the error if one occurs.
def getDescriptionOf(): # params = uri
	res = None
	if len(sys.argv) == fp+1:
		res = NAO.getDescriptionOf(uri=sys.argv[fp+0])
	else:
		print "bad usage..."
		print "usage: "
		print "\tpython_calls results_path function_name [param1 param2 ...]"
		exit(1)
	return res
	
# expected output: registered in tagsCache.rdf.
# - can directly be used by a tree view in XUL.
def getTagsOf(): # params = uri
	res = None
	if len(sys.argv) == fp+1:
		res = NAO.getTagsOf(uri=sys.argv[fp+0])
	else:
		print "bad usage..."
		print "usage: "
		print "\tpython_calls results_path function_name [param1 param2 ...]"
		exit(1)
	return res
	
# expected output: registered in tagsCache.rdf.
# - can directly be used by a tree view in XUL.
def getAllTags(): # no param
	res = None
	if len(sys.argv) == fp+0:
		res = NAO.getAllTags()
	else:
		print "bad usage..."
		print "usage: "
		print "\tpython_calls results_path function_name [param1 param2 ...]"
		exit(1)
	return res
	
# error-display function
def error():
	print "The method \"" + method_name + "\" does not exist."


functions = {
	"addTag": addTag,
	"removeTag": removeTag,
	"getTagsOf": getTagsOf,
	"setDescription": setDescription,
	"removeDescription": removeDescription,
	"getDescriptionOf": getDescriptionOf,
	"getAllTags": getAllTags
}
res = functions.get(method_name, error)()
# do nothing with the result

